package com.lingchen.boot.utils;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

import org.springframework.util.Base64Utils;

public class TestUtil {

    /**
     * 加密
     * json-》byte array
     * byte array ->gzip byte array
     * gzip byte array -> base64 -> String utf-8
     * String [0:len/3] +一个随机字符+[len/3:len/2]+一个随机字符+[len/2:len]
     *
     * 以下是反向解密过程
     * @param args
     */
    public static void main(String[] args) {

        String api = "https://api.manmanapp.com/v1/";
        String method = "POST";
        String paramName = "content";
        //String paramValue = "H4sIAAAAAAAAAD2KwQrCQAxE/yVnWRe0TerZa2/el62bQlATabsiiP/e2ICPMwDBv5gOL3VjhBI+s7jOPud6XywZ3UkJ/i03bZixZ+O3vxNItpulphHw9IDkVH+5WAgVJf+5qylsmk/PMYXClGwq7FhjrCtolpyFIqfFfLiJHUhwAAAA==";
        String paramValue = "H4sIAAAAAAAAAD2KwQrCQAxE/yVnWRe0TerZa2/el62bQlATabsiiP/e2IQPMwDBv5gOL3VjhBI+s7jOPud6XywZ3HkJ/i03bZixZ+O3vxNItpulphHw9IDkVH+5WAgVJf+5qylsmk/PMYXClGwq7FhjrCtolpyFIqfFfLiJHUhwAAAA==";

        int len = paramValue.length() - 2;
        int len1 = len / 3;
        int len2 = len / 2;
        String s1 = paramValue.substring(0, len1);
        String s2 = paramValue.substring(len1 + 1, len2 + 1);
        String s3 = paramValue.substring(len2 + 2, paramValue.length());
        String real = s1 + s2 + s3;

        byte[] array = Base64Utils.decode(real.getBytes());

        try {
            GZIPInputStream g = new GZIPInputStream(new ByteArrayInputStream(array));
            ByteArrayOutputStream b = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            while (g.read(buffer) != -1) {
                b.write(buffer);
            }
            byte[] result = b.toByteArray();
            b.close();
            g.close();
            System.out.println(new String(result));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


}
